package binpacking.io;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import binpacking.model.*;



public class InputLoader {
	
	private String blockFile;
	private String binFile;
	
	private ArrayList<Block> blocks;
	private BinContainer binContainer;
	private int binSize;
	
	public InputLoader( String blockFile, String binFile ){
		this.blockFile = blockFile;
		this.binFile = binFile;
		
		this.blocks = new ArrayList<Block>();
		this.binContainer = new BinContainer();
	}
	
	public boolean load(){
		
		// remove previous runs
		blocks.clear();
		binContainer.getBins().clear();
		
		try{
			BufferedReader in = new BufferedReader( new FileReader( blockFile ) );
            String line = "";
            
            while( (line = in.readLine()) != null) {
            	if( !line.isEmpty() ){
            		blocks.add( new Block( Integer.parseInt(line) ) );
            	}
            }
            
            in = new BufferedReader( new FileReader( binFile ) );
            line = "";
            int aantalBins = 0;
            
            while( (line = in.readLine()) != null) {
            	if( !line.isEmpty() ){
	            	String[] splitted = line.split(" ");
	            	aantalBins = Integer.parseInt( splitted[0] );
	            	binSize = Integer.parseInt( splitted[1] );
            	}
            }
            
            for(; aantalBins > 0; aantalBins--) {
            	binContainer.getBins().add( new Bin( binSize ) );
            }
            
		}
		catch (Exception e) {
			System.err.println( "Exception: " + e.getMessage() );
			return false;
		}
		return true;
	}
	
	public ArrayList<Block> getBlocks() {
		return blocks;
	}

	public BinContainer getBinContainer() {
		return binContainer;
	}

	public int getBinSize() {
		return binSize;
	}

}
